package com.zb.code.controller;


import com.zb.annotation.RequestLimit;
import com.zb.annotation.SystemLog;
import com.zb.code.service.ConCodeService;
import com.zb.constant.CodeConstant;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.CodeParamInfo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjCodeService;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 编码表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-08-23
 */
@Api(tags = "编码类接口")
@RestController
@RequestMapping("/code")
public class ZjCodeController {
    @Resource
    private ConCodeService conCodeService;
    @Resource
    private ZjCodeService codeService;

    @RequestLimit(count = 2)
    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, remark = "编码档案-编码列表 - 新增编码")
    @ApiOperation("新增编码")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid CodeBatchVo codeBatchVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conCodeService.save(loginInfo, codeBatchVo, new CodeParamInfo()
                .setCodeSource(CodeConstant.CODE_SOURCE_1)
                .setTemporary(false));
        return Result.success("新增编码成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.SELECT, remark = "编码档案-编码列表 - 查询编码列表")
    @ApiOperation("查询编码列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody @Valid CodeListSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("查询编码列表成功").data(codeService.getList(custAccId, selectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.SELECT, remark = "编码档案-编码列表 - 查询编码列表详情")
    @ApiOperation("查询编码列表详情")
    @GetMapping("/getInfo/{id}")
    public Result getInfo(@PathVariable @ApiParam(value = "编码ID", required = true) Long id) {
        return Result.success("获取编码列表详情成功").data("row", conCodeService.getInfo(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.UPDATE, remark = "编码档案-编码列表 - 批量激活编码")
    @ApiOperation("批量激活编码")
    @PutMapping("/activCode")
    public Result activCode(HttpServletRequest request, @RequestBody @Valid List<CodeParamVo> codeParamVos){
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        codeService.updateBatchActiveByIds(loginInfo, codeParamVos);
        return Result.success("编码激活成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.DELETE, remark = "编码档案-编码列表 - 批量删除编码")
    @ApiOperation("批量删除编码")
    @DeleteMapping("/deleteCode")
    public Result deleteCode(@RequestBody @Valid List<CodeParamVo> codeParamVos) {
        conCodeService.deleteBatchByIds(codeParamVos);
        return Result.success("编码删除成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.UPDATE, remark = "编码档案-编码列表 - 批量激活编码")
    @ApiOperation("更改编码商品信息")
    @PutMapping("/codeGoodsBindingChange")
    public Result codeGoodsBindingChange(HttpServletRequest request, @RequestBody @Valid CodeGoodsChangeVo goodsChangeVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        codeService.codeGoodsBindingChange(loginInfo, goodsChangeVo);
        return Result.success("编码商品信息变更成功");
    }

    @ApiOperation("根据编码ID获取编码的历史工单")
    @PostMapping("/getHistoryOrder/{codeId}")
    public Result getHistoryOrder(@PathVariable @ApiParam(value = "编码ID", required = true) Long codeId,
                                  @RequestBody PageUtil pageUtil) {
        return Result.success("获取编码历史工单成功").data(conCodeService.getHistoryOrder(codeId, pageUtil));
    }

    @ApiOperation("获取未登记商品编码")
    @PostMapping("/getNotRegisterCode")
    public Result getNotRegisterCode(HttpServletRequest request, @RequestBody OrderCodeSelectVo codeSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取未登记商品编码成功").data(conCodeService.getNotRegisterCode(custAccId, codeSelectVo));
    }

    @ApiOperation("根据客户ID获取已登记编码")
    @PostMapping("/getRegisterCode/{userId}")
    public Result getRegisterCode(HttpServletRequest request, @PathVariable @ApiParam(value = "客户ID", required = true) Long userId,
                                  @RequestBody OrderCodeSelectVo codeSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取已登记编码成功").data(conCodeService.getRegisterCode(custAccId, userId, codeSelectVo));
    }

}
